package com.duck.user.controller;

import com.duck.user.service.UserFollowService;
import com.duck.common.domain.Result;
import com.duck.common.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserFollowController {

    @Autowired
    private UserFollowService userFollowService;

    /**
     * 关注用户
     * @param followingId 被关注者的 ID
     * @return Result 返回关注成功或失败状态
     */
    @PostMapping("/follow")
    public Result followUser(@RequestParam("followingId") Long followingId) {
        String followerIdStr = String.valueOf(UserContext.getUser());
        Long followerId = Long.valueOf(followerIdStr);
        try {
            userFollowService.followUser(followerId, followingId);
            return new Result(200, "关注成功");
        } catch (Exception e) {
            return new Result(404, "关注失败: " + e.getMessage());
        }
    }

    /**
     * 取消关注
     * @param followingId 被取消关注者的 ID
     * @return Result 返回取消关注状态
     */
    @PostMapping("/unfollow")
    public Result unfollowUser(@RequestParam("followingId") Long followingId) {
        String followerIdStr = String.valueOf(UserContext.getUser());
        Long followerId = Long.valueOf(followerIdStr);
        try {
            userFollowService.unfollowUser(followerId, followingId);
            return new Result(200, "取消关注成功");
        } catch (Exception e) {
            return new Result(404, "取消关注失败: " + e.getMessage());
        }
    }

    /**
     * 查询关注数（即我关注了多少人）
     * @param userId 用户 ID
     * @return Result 返回关注数
     */
    @GetMapping("/countFollowing")
    public Result countFollowing(@RequestParam("userId") Long userId) {
        int count = userFollowService.countFollowing(userId);
        return new Result(count, 200, "查询成功");
    }

    /**
     * 查询粉丝数（即有多少人关注我）
     * @param userId 用户 ID
     * @return Result 返回粉丝数
     */
    @GetMapping("/countFollowers")
    public Result countFollowers(@RequestParam("userId") Long userId) {
        int count = userFollowService.countFollowers(userId);
        return new Result(count, 200, "查询成功");
    }

    /**
     * 获取关注列表（返回被关注者 ID 列表）
     * @param userId 用户 ID
     * @return Result 返回关注列表
     */
    @GetMapping("/listFollowing")
    public Result listFollowing(@RequestParam("userId") Long userId) {
        List<Long> followingList = userFollowService.getFollowingList(userId);
        return new Result(followingList, 200, "查询成功");
    }

    /**
     * 获取粉丝列表（返回关注者 ID 列表）
     * @param userId 用户 ID
     * @return Result 返回粉丝列表
     */
    @GetMapping("/listFollowers")
    public Result listFollowers(@RequestParam("userId") Long userId) {
        List<Long> followersList = userFollowService.getFollowersList(userId);
        return new Result(followersList, 200, "查询成功");
    }
}
